/**
 *
 */
package com.hybris.msig.paymentgateway.facade.impl;

import de.hybris.platform.converters.impl.AbstractPopulatingConverter;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.hybris.msig.paymentgateway.data.PaymentResponseData;
import com.hybris.msig.paymentgateway.dto.MsigPayRequestDto;
import com.hybris.msig.paymentgateway.dto.MsigPaymentCheckStatusDto;
import com.hybris.msig.paymentgateway.facade.MsigCommonPaymentFacade;
import com.hybris.msig.paymentgateway.model.PaymentResponseModel;


/**
 * @author i313922
 *
 */
@Component
public class DefaultMsigCommonPaymentFacade implements MsigCommonPaymentFacade
{
	@Resource
	private FlexibleSearchService flexibleSearchService;

	@Resource
	private ModelService modelService;

	@Resource
	private AbstractPopulatingConverter<PaymentResponseModel, PaymentResponseData> msigPaymentResponseConverter;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.hybris.msig.paymentgateway.facade.MsigCommonPaymentFacade#checkPaymentStatus(java.lang.String)
	 */
	@Override
	public PaymentResponseData checkPaymentStatus(final MsigPaymentCheckStatusDto msigPaymentCheckStatusDto)
	{
		final PaymentResponseModel condition = new PaymentResponseModel();
		condition.setRefid(msigPaymentCheckStatusDto.getRefid());
		final PaymentResponseModel result = flexibleSearchService.getModelByExample(condition);
		return msigPaymentResponseConverter.convert(result);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.hybris.msig.paymentgateway.facade.MsigCommonPaymentFacade#initPaymentTransaction(com.hybris.msig.
	 * paymentgateway.dto.MsigPayRequestDto)
	 */
	@Override
	public void initPaymentTransaction(final MsigPayRequestDto msigPayRequestDto)
	{
		final PaymentResponseModel transaction = new PaymentResponseModel();
		transaction.setRefid(msigPayRequestDto.getRefid());
		transaction.setStatus("NEW");
		modelService.save(transaction);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.hybris.msig.paymentgateway.facade.MsigCommonPaymentFacade#updatePaymentTransaction(com.hybris.msig.
	 * paymentgateway.data.PaymentResponseData)
	 */
	@Override
	public void updatePaymentTransaction(final PaymentResponseData paymentResponseData)
	{
		final PaymentResponseModel condition = new PaymentResponseModel();
		condition.setRefid(paymentResponseData.getRefid());
		final PaymentResponseModel paymentResponseModel = flexibleSearchService.getModelByExample(condition);
		paymentResponseModel.setStatus(paymentResponseData.getStatus());
		paymentResponseModel.setMerchantBank(paymentResponseData.getMerchantBank());
		paymentResponseModel.setGtwTimestamp(paymentResponseData.getGtwTimestamp());
		paymentResponseModel.setGtwAckID(paymentResponseData.getGtwAckID());
		modelService.save(paymentResponseModel);
	}
}
